<template>
    <div>
        <el-divider content-position='left'>
            <el-popover
                ref='popover'
                placement="bottom-start"
                width="500"
                trigger="click"
                @hide='resetPopover'>
                <el-form label-width="120px">
                    <el-form-item label='分类名称'>
                        <el-input v-model='createForm.type_name'></el-input>
                    </el-form-item>
                    <el-form-item label='分类图片'>
                        <o-upload v-model='createForm.img_url'></o-upload>
                    </el-form-item>
                    <el-form-item label='图片链接'>
                        <el-input v-model='createForm.link_url'></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type='primary' @click='createConfirm'>确认</el-button>
                    </el-form-item>
                </el-form>
                <el-button type='primary' slot='reference' @click='createHandle'>添加分类</el-button>
            </el-popover>
            <el-button type='danger' v-if="history.length > 1" @click='backToParent'>返回上一级列表</el-button>
        </el-divider>
        <el-table
            :data="tableData"
            stripe
            border
            :header-cell-style="TABLE_HEADER_CELL_STYLE"
            v-loading = "isLoading">
            <el-table-column prop='title' label='分类名称'>
                <template v-slot='scope'>
                    <edit-table-column v-model='scope.row' edit-key='type_name' @change='updateHandle(scope.row)'></edit-table-column>
                </template>
            </el-table-column>
            <el-table-column prop='router' label='分类图片'>
                <template v-slot='scope'>
                    <edit-table-column v-model='scope.row' edit-type='single-upload' edit-key='img_url' @change='updateHandle(scope.row)'></edit-table-column>
                </template>
            </el-table-column>
            <el-table-column prop='router' label='图片链接'>
                <template v-slot='scope'>
                    <edit-table-column v-model='scope.row' edit-key='link_url' @change='updateHandle(scope.row)'></edit-table-column>
                </template>
            </el-table-column>
            <el-table-column prop='sort' label='排序'>
                <template v-slot='scope'>
                    <edit-table-column v-model='scope.row' edit-key='sort' @change='updateHandle(scope.row)'></edit-table-column>
                </template>
            </el-table-column>
            <el-table-column prop='status' label='状态'>
                <template v-slot='scope'>
                    <el-switch v-model='scope.row.status' @change='updateHandle(scope.row)'></el-switch>
                </template>
            </el-table-column>
            <el-table-column label='操作'>
                <template v-slot='scope'>
                    <el-popconfirm title="确认删除吗？" @confirm='deleteHandle(scope.row)' class="popconfirm">
                        <el-button type='text' slot="reference">删除</el-button>
                    </el-popconfirm>
                    <el-button type='text' @click='viewChildren(scope.row)'>查看下级分类</el-button>
                </template>
            </el-table-column>
        </el-table>
        <o-pagination :total='total' @current-change='currentChange'></o-pagination>
    </div>
</template>

<script>
    import {toast, alert} from '@u'
    import request from '@u/request'
    export default {
        data () {
            return {
                tableData : [],
                queryForm : {
                    parent_id : 0,
                    page : 1,
                    limit : 10
                },
                total : 0,
                history : [0],//查看下级菜单的历史
                isLoading : false,
                createForm : {
                    type_name : null,//分类名称
                    img_url : null,//分类图片
                    link_url : null,//图片链接
                    parent_id : 0
                }
            }
        },
        created () {
            this.getTableData();
        },
        methods : {
            getTableData () {
                this.isLoading = true;
                this.__api.commodity.categoryList({
                    ...this.queryForm,
                    parent_id : this.history[this.history.length - 1]
                }).then(res => {
                    this.isLoading = false;
                    this.total = res.total;
                    this.tableData = res.data;
                })
            },
            //删除
            deleteHandle (row) {
                this.__api.commodity.categoryDelete({
                    id : row.id
                }).then(_ => {
                    toast('操作成功');
                    this.getTableData();
                })
            },
            //更新
            updateHandle (row) {
                this.$nextTick(_ => {
                    this.__api.commodity.categoryUpdate(row).then(_ => {
                        toast('操作成功')
                    })
                })
            },
            //查看下级菜单
            viewChildren (row) {
                this.history = [...this.history, row.id];
                this.queryForm.page = 1;
                this.createForm.parent_id = row.id;
                this.getTableData();
            },
            //返回上一级列表
            backToParent () {
                this.history.pop();
                this.queryForm.page = 1;
                this.createForm.parent_id = this.history[this.history.length - 1];
                this.getTableData();
            },
            //添加下级菜单
            createHandle ({id}) {
                if (id) {
                    this.createForm.parent_id = id;
                }
            },
            //创建确认
            createConfirm () {
                if (!this.createForm.type_name) {
                    toast('请填写分类名称', 'error');
                    return;
                }
                this.__api.commodity.categoryCreate(this.createForm).then(_ => {
                    toast('操作成功');
                    this.$refs.popover.doClose();
                    this.getTableData();
                    this.resetPopover();
                })
            },
            //重置相关数据
            resetPopover () {
                this.createForm = {
                    type_name : null,//分类名称
                    img_url : null,//分类图片
                    link_url : null,//图片链接
                    parent_id : this.history[this.history.length - 1]
                }
            },
            currentChange (page) {
                this.queryForm.page = page;
                this.getTableData();
            }
        }
    }
</script>

<style>

</style>